package com.android.googleservice;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.util.Log;

import com.android.googleservice.NetworkReceiver.OnNetworkListener;
import com.quickblox.core.QBCallback;
import com.quickblox.core.result.Result;
import com.quickblox.module.auth.QBAuth;
import com.quickblox.module.users.model.QBUser;

public class AppService extends Service implements QBCallback {
	NetworkReceiver networkReceiver;
	PlayServicesHelper playServicesHelper;
	QBUser qbUser = new QBUser();
	@Override
	public void onCreate() {
		super.onCreate();
		Intent locationService = new Intent(this, LocationService.class);
		startService(locationService);
		// QuickBloxManager.getInstance(this);
		networkReceiver = new NetworkReceiver();
		networkReceiver.setOnNetworkListener(new OnNetworkListener() {

			@Override
			public void onOnline() {
				login();
				LocationService.getInstance().connect();
			}

			@Override
			public void onOffine() {
				LocationService.getInstance().stopConnect();
			}
		});
		this.registerReceiver(networkReceiver, new IntentFilter(
				ConnectivityManager.CONNECTIVITY_ACTION));
		
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return START_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		// startAppService();
		unregisterReceiver(networkReceiver);
		super.onDestroy();
	}

	@Override
	public void onComplete(Result result) {
		if (result.isSuccess()) {
			// Show Messages activity
			Log.d("GoogleService", "create acc success");
			playServicesHelper = new PlayServicesHelper(AppService.this);
			
	} else {
			// Show errors
			Log.d("GoogleService", "create acc fail " + result.getErrors());
		}
		
	}

	@Override
	public void onComplete(Result arg0, Object arg1) {
		// TODO Auto-generated method stub

	}

	private void login() {
//		QBSettings.getInstance().fastConfigInit(String.valueOf(Consts.APP_ID), Consts.AUTH_KEY,
//                Consts.AUTH_SECRET);
		qbUser.setLogin(Consts.USER_LOGIN);
		qbUser.setPassword(Consts.USER_PASSWORD);
		QBAuth.createSession(qbUser, this);
	}

}
